   var green_icon = getIcon("green");


         // 取URL中的参数q---手机号
         function getParameter(param)
         {
            var query = window.location.search;
            var iLen = param.length;
            var iStart = query.indexOf(param);

            if (iStart == -1)
               return "";
            iStart += iLen + 1;
            var iEnd = query.indexOf("&", iStart);
            if (iEnd == -1)
           　 return query.substring(iStart);
            return query.substring(iStart, iEnd);
         }

         var intervalID = "-1";
         var isFirst = true;
         var mapType = "";
         var freq = parent.document.getElementById("freq").value
// alert(freq);
       var from = parent.document.getElementById("from").value;
       var initFrom;
       var to = parent.document.getElementById("to").value;
       var objectID = parent.document.getElementById("objectID").value;
       
// alert("sdf");
       var Location = "GetHistoryOneByOne?userID="+objectID+"&mapType="+mapType+"&pwd=123456&from="+from+"&to="+to;
       var responsetext;
       var elements;
       var stopTime = 0, runTime = 0;
       var intervalID2 = -1;
       var lastTime;
       
       function ajaxRequest(){
           if(elements == null)
    	   	from = parent.document.getElementById("from").value;
           else
               from = elements[2];
           
           var myAjax = new Ajax.Request("GetDataService", {
        	            method:"post",       // 表单提交方式
        	            parameters:"method=getHistoryMByM&userID="+objectID+"&mapType="+mapType+"&pwd=123456&from="+from+"&to="+to,
        	            asynchronous:false,
        	            setRequestHeader:{"If-Modified-Since":"0"},     // 禁止读取缓存数据
        	            onComplete:function(x){    // 提交成功回调
        	            	responsetext = x.ResponseText;
        	            	// alert(responsetext);
    if(isFirst){
        	                // 没有数据，提示并返回
        	                if(responsetext == null || responsetext == ""){
        	                    alert("没有查询到这个时段的数据");
        	                    return;
        	                }
        	                initFrom = from;
        	                isFirst = false;
        	                
    }else{
        if(responsetext == null || responsetext == ""){
            window.clearInterval(intervalID2);
            alert("轨迹回放完毕，请查看!!!时间从 " + initFrom +" 到 " + to+ "<"+pointNum+">");
            var totalTime = getTimeDiff2(to, initFrom);
            document.getElementById("markerText").innerHTML = "<span id='markerText'><font size='2'>总时间：" 
                + exchange_time(totalTime) + "<"+pointNum+"><br>总里程："+formatDistance_(len)
                +"<"+glPoints+"><br>行驶："+exchange_time(runTime/*totalTime-stopTime*/)+"<br>停留："
                +exchange_time(totalTime-runTime/*stopTime*/)+"</font></span>";

                 parent.document.getElementById("command").value = "开始回放";
            return;
        }
    	
    }    	  
							// 得到和上一点的时间间隔
    
							if(elements == null)
								lastTime = from;	
							//else	
								//lastTime = elements[2];	
								
var elementss = responsetext.split(";");

var currIndex= 0;
// alert("dfds" + currIndex);
intervalID2=window.setInterval(function() {
	// alert(intervalID2);
	if(currIndex == elementss.length -2){
		window.clearInterval(intervalID2);
		ajaxRequest();
		if(responsetext == null || responsetext == ""){//如果是最后一次请求
			window.clearInterval(intervalID2);
			return;
		}
			
	}
	// alert(currIndex);
	drawOne(elementss[currIndex]);
	++ currIndex;
	
},freq);

        	                
        	            },
        	            onError:function(x){          // 提交失败回调
        	                    alert(x.statusText);
        	            } 
        	        } 
        	   ); 
           }
var paoYiCount = 0;//连续漂移点
var preTime = 0;//前一点时间 
var preSpeed = 0;
function showGuoLv(){
	paoYiCount ++;
    document.getElementById("markerText").innerHTML = "<span id='markerText'><font size='2'>速度：" 
        /* elements[3]*/ + "0Km/h<"+pointNum+"><br>总里程："+formatDistance_(len)
        +"<br>定位："+elements[2].substring(5, elements[2].length -2)+"<br>停留："+exchange_time(stopTime)+"</font></span>";
    marker.showPopup();
}
function drawOne(oneLine){
	if(elements == null)
		preTime = lastTime;		
	else{
		preTime = elements[2];
		preSpeed = elements[3];
	}
	elements=oneLine.split(","); 
	if(map == null){
		// alert("map is null");
		x0=elements[1];
	    y0=elements[0];
		initMap();
	}

	if(x0 ==0 && y0 == 0){
		initMap();
//		initRestart();
//		lastTime = elements[2];
	}
	
    timeDiff = getTimeDiff2(elements[2], lastTime);

/*
	if(getTimeDiff2(elements[2], preTime) > 30){//过滤间隔时间超过20S的数据
		showGuoLv(); return;
	}
	
    if(elements[3] < 5){	//过滤速度小于5的数据。
    	showGuoLv(); return;
	}
	
	if(elements[3] < 10 && paoYiCount > 5){
		showGuoLv(); return;
	}

	if(elements[3] >= 10 && elements[3] < 15 && paoYiCount > 8 && preSpeed < 5){
		showGuoLv(); return;
	}
	*/
	
    if(timeDiff > 180){//考虑在速度比较慢的情况下，会隔点传。
    	stopTime += timeDiff;
    	//paoYiCount ++;
    }else{
    	runTime += timeDiff;
    	//paoYiCount = 0;    	
    }
    
    paoYiCount = 0; 
//    alert(lastTime);

    // else
    	// stopTime = 0;
    drawLine();
    // parent.document.getElementById("from").value = elements[2];

    lastTime = elements[2];
 // alert(parent.document.getElementById("from").value);
}
       var map;
       var x,y,x0,y0,hx=0;
       var center, point;
       var opts;
       var marker;
       var initText;
       
 		function initMap(){
 			var initSize = 0;
            if(map == null){
            	initSize = 12;
            	map = new GMap2(document.getElementById("map"));
            	var mapControl = new GMapTypeControl();
                map.addControl(mapControl);
                map.removeMapType(G_SATELLITE_MAP);
                map.addControl(new GLargeMapControl());
                map.addControl(new GScaleControl());
            }
            else
            	map.clearOverlays();
            x=Number(elements[1]);
            y=Number(elements[0]);
            x0=x;
            y0=y;
            center = new GLatLng(x, y);
            point=center;
            if(initSize == 12)
            	map.setCenter(center, 12);
            else
            	map.setCenter(center);
           
/*
 * GEvent.addListener(map, "maptypechanged", function(){
 * 
 * //切换地图类型 window.clearInterval(intervalID2); mapType =
 * (map.getCurrentMapType().getName() == "卫星") ? "G_SATELLITE_MAP" : "";
 * map.clearOverlays(); from = parent.document.getElementById("from").value;
 * ajaxRequest(); x=Number(elements[1]); y=Number(elements[0]); point=new
 * GLatLng(x, y); x0=x; y0=y; len = 0, pointNum = 0; opts = { text : initText,
 * icon : green_icon}; marker = new PopupMarker(point, opts);
 * map.addOverlay(marker); document.getElementById("markerText").innerHTML = "<span
 * id='markerText'><font size='2'>速度： " + elements[3] + "公里/小时<br>总里程："+formatDistance_(len)+"<br>位置点："+pointNum+"<br>停留："+exchange_time(stopTime)+"</font></span>";
 * map.setCenter(point); });
 */          
            


            initText = "<span id='markerText'><font size='2'>速度： ................公里/小时....<br>总里程：......<br>位置点：.....................<br>停留时间：.......</font></span>";
            // var marker = new GMarker(center, {draggable: false});
            opts = { text : initText, icon : green_icon};
        	marker = new PopupMarker(center, opts);
            map.addOverlay(marker);

            GEvent.addListener(marker, "mouseover", function(){
          	   marker.showPopup();
               });
             GEvent.addListener(marker, "mouseout", function(){
                 marker.hidePopup();
               });
            
            document.getElementById("markerText").innerHTML = "<span id='markerText'><font size='2'>速度： " + elements[3] + "公里/小时<br>总里程："+formatDistance_(len)+"<br>位置点："+pointNum+"<br>停留："+exchange_time(stopTime)+"</font></span>";
    		}      

 		var len = 0, pointNum = 0;

 		function initRestart(){
 			// window.clearInterval(intervalID2);
 			map.clearOverlays(); 
 			x=Number(elements[1]);
            y=Number(elements[0]);
            x0=x;
            y0=y;
            center = new GLatLng(x, y);
            point=center;
            // map.setCenter(center, 12);
            opts = { text : initText, icon : green_icon};
            marker = new PopupMarker(point, opts);
            map.addOverlay(marker);
            document.getElementById("markerText").innerHTML = "<span id='markerText'><font size='2'>速度： " + elements[3] + "公里/小时<br>总里程："+formatDistance_(len)+"<br>位置点："+pointNum+"<br>停留时间："+exchange_time(stopTime)+"</font></span>";
            map.setCenter(point);       

            // 画第一个静止点 ？？timeDiff 从哪里来的？
    		if(timeDiff > 179){
    			markStopPoint(point);
    		}            
    		}


 		var stopIcon = getIcon("gray");
 		
 		function markStopPoint(point){
//alert(exchange_time(timeDiff));
			var startTime = getFormatDateTime(lastTime);
			var endTime = getFormatDateTime(elements[2]);
			var stopOpts = { text : "<font size='2'>停留时长："+exchange_time(timeDiff)+"<br>开始："+startTime+"<br>结束："+endTime+"</font>", icon:stopIcon};//
			var stopMarker = new PopupMarker(point, stopOpts);// new
																// GMarker((point),{draggable:
																// false,icon:stopIcon,title:"停留"+exchange_time(timeDiff)+"\r\n从"+startTime+"到"+endTime+""});
			map.addOverlay(stopMarker);
            GEvent.addListener(stopMarker, "mouseover", function(){
            	stopMarker.showPopup();
                });
              GEvent.addListener(stopMarker, "mouseout", function(){
            	  stopMarker.hidePopup();
                });
              stopMarker.hidePopup();
 		}
 
 		var glPoints = 0;
 		
 		function drawLine(){
    		x=elements[1];
            y=elements[0];
            if(timeDiff > 600){
            	markStopPoint(new GLatLng(x0, y0));
            }
            
            //过滤完全相同的点
            if(x == x0)
            	if(y == y0)
            		return;

 
            
            point=new GLatLng(x, y);

if(hx-elements[4] < 2 && hx-elements[4] > -2){
	// alert("....");
	glPoints ++;
	return;
}
         hx = elements[4];   
         
 //if (Math.sqrt((x-x0)*(x-x0)+(y-y0)*(y-y0))>0.0002) {           
               // map.panTo(point);
               
        var color = "#00FF00";
		if (elements[3] > 80)
			color = "#FF00FF";
		if (elements[3] >119 )
			color = "#FF0000";
		
		 //if(timeDiff > 600){ color = "black"; }
		 
               
               
               // map.addOverlay(marker);
               var polyline = new GPolyline([new GLatLng(x0, y0), new GLatLng(x, y),], color, 5);
               len += new GLatLng(x0, y0).distanceFrom(new GLatLng(x, y));
               pointNum ++;
               x0=x;
               y0=y;
               map.addOverlay(polyline);
               document.getElementById("markerText").innerHTML = "<span id='markerText'><font size='2'>速度：" 
                   + elements[3] + "Km/h<"+pointNum+"><br>总里程："+formatDistance_(len)
                   +"<br>定位："+elements[2].substring(5, elements[2].length -2)+"<br>停留："+exchange_time(stopTime)+"</font></span>";
               marker.setLatLng(point);
               marker.showPopup();
               
               
               //判读是否该居中
               if(!map.getBounds().containsLatLng(point))
            	   map.panTo(point);
 }
 //       }		

        function reset(){
            x0=0;y0=0;len=0;stopTime=0;runTime=0;// pointNum=0;
            initFrom = parent.document.getElementById("from").value;
            elements = null;
            
        }